package cn.bonoon.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bonoon.kernel.menus.IModuleInfo;
import cn.bonoon.kernel.menus.ModuleManager;
import cn.bonoon.kernel.security.AuthenticatedUserDetails;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.support.models.Item;
import cn.bonoon.kernel.support.services.UserService;

@Controller
@RequestMapping("/s/common")
public class ComboBoxController extends AuthenticatedUserDetails {

	@Autowired
	private ModuleManager moduleManager;
	@Autowired
	private UserService userService;

	// 只显示用户被分配的角色的子系统
	@ResponseBody
	@RequestMapping("modules/items.do")
	public List<Item> modules() {
		List<Item> items = new ArrayList<Item>();
		LogonUser user = getUser();

		if (user.isSuper()) {
			items.add(new Item(moduleManager.getId(), moduleManager.getName()));
			for (IModuleInfo module : moduleManager.modules()) {
				items.add(new Item(module.getId(), module.getName()));
			}
		} else {
			for (String key : userService.accessableModules(user)) {
				IModuleInfo module = moduleManager.module(key);
				if (null != module) {
					items.add(new Item(module.getId(), module.getName()));
				}
			}
		}

		return items;
	}


}
